package com.huawei.douyue.Controller;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

@RestController
public class BaseController {
        @GetMapping("/video/{id}")
        public ResponseEntity<InputStreamResource> getVedio(@PathVariable("id") Integer id, HttpServletResponse response) throws FileNotFoundException {
            File videoFile;
            if(id.equals(1)){
                videoFile = new File("/Users/chenhan/Downloads/1.mp4");
            }else if(id.equals(2)){
                videoFile = new File("/Users/chenhan/Downloads/2.MOV");
            }else{
                videoFile = new File("/Users/chenhan/Downloads/3.mp4");
            }

            response.setContentType(MediaType.parseMediaType("video/mp4").toString());
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION,"inline; filename=video.mp4");
            InputStreamResource resource = new InputStreamResource(new FileInputStream(videoFile));
            return ResponseEntity.ok()
                    .contentType(MediaType.parseMediaType("video/mp4"))
                    .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=video.mp4")
                    .body(resource);
        }







}
